//
//  UseCarTypeCell.m
//  NtoccShipper
//
//  Created by mac on 2019/12/3.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "UseCarTypeCell.h"
#import "UseCarTypeModel.h"

@interface UseCarTypeCell(){
    UIView* _imgParent;
//    int index;  // 记录图片的位置
}
@property (nonatomic,strong) NSMutableArray *arrModel;
@property (nonatomic,strong) CJLabel *labContent;

@end

@implementation UseCarTypeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



-(void)removeCell:(UIGestureRecognizer*)sender
{
    
    UIView* cell = sender.view;
    UILabel *label = (UILabel *)[cell viewWithTag:1000 + cell.tag - 100];
    UseCarTypeModel *model = self.arrModel[cell.tag - 100];

    int indexS = 0;
    
//    [self.arrModel enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//
//        UseCarTypeModel *mod = (UseCarTypeModel *)obj;
//
//        if (mod.selected) {
//            int i = 0;
//            indexS++;
//
//        }
//
//    }];
    
 
    
    if (model.selected) {
        model.selected = NO;
        cell.backgroundColor = [UIColor cjWhiteColor];
        label.textColor = [UIColor cjMainTextColor];

    }else{
        model.selected = YES;
        cell.backgroundColor = [UIColor cjMainColor];
        label.textColor = [UIColor cjWhiteColor];
    }
    
    for (int i = 0; i < self.arrModel.count; i++ ) {
         UseCarTypeModel *mod = self.arrModel[i];
         if (mod.selected) {
             indexS++;
             
             if (indexS >= 4 ) {
                 [CJAlert showStringDismissWithDelay:@"只能选3个"];
                 model.selected = NO;
                 cell.backgroundColor = [UIColor cjWhiteColor];
                 label.textColor = [UIColor cjMainTextColor];
                 return;
             }
         }

     }
}

- (void)setCellInfoWithMutaArray:(NSMutableArray *)arrMuta index:(NSInteger )index{
    
    
        [_imgParent removeAllSubviews];
    //    index = 0;
//        [arrMuta reverse];
        for (int i =0; i < arrMuta.count; i++) {
            
            if (index == 0) {
                self.labContent.text = @"车长(最多选3个)";
            }else{
                self.labContent.text = @"车型(最多选3个)";

            }
            UseCarTypeModel *model = arrMuta[i];

                UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeCell:)];

                UIView* cell = [[UIView alloc]init];
                cell.tag = i + 100;
                [cell enableFlexLayout:YES];
                [cell addGestureRecognizer:tap];

                [cell setLayoutAttrStrings:@[
                                           @"minHeight",@"30",
                                           @"margin",@"1.5%",
                                           @"alignItems",@"center",
                                           @"justifyContent",@"center",

                                           ]];
                [cell setViewAttr:@"borderRadius" Value:@"5"];
                [cell setViewAttr:@"borderWidth" Value:@"0.5"];
                [cell setViewAttr:@"borderColor" Value:@"#E4E9ED"];


                UILabel *lab = [[UILabel alloc]init];
                lab.tag = 1000 + i;
                [lab enableFlexLayout:YES];
                [lab setLayoutAttrStrings:@[

                                     @"flex",@"1",
                                     ]];

                [cell addSubview:lab];
                //          [lab setViewAttr:@"textAlign" Value:@"center"];
                [lab setViewAttr:@"fontSize" Value:@"14"];
                [lab setViewAttr:@"color" Value:@"#434C67"];

                lab.text = CJStringWithFormat(@"  %@  ",model.name);
                NSAttributedString * attR = [[NSAttributedString alloc] initWithString:lab.text attributes:@{NSFontAttributeName: lab.font,NSBaselineOffsetAttributeName:@(2)}];
                [lab setAttributedText:attR];

                [cell markDirty];
                [_imgParent insertSubview:cell atIndex:0];

                [_imgParent markDirty];

                [self layoutIfNeeded];
                

            if (model.selected) {
                 cell.backgroundColor = [UIColor cjMainColor];
                 lab.textColor = [UIColor cjWhiteColor];
             }else{
                 cell.backgroundColor = [UIColor cjWhiteColor];
                 lab.textColor = [UIColor cjMainTextColor];
             }
                    
        }
        
        self.arrModel = [[NSMutableArray alloc]init];
        self.arrModel = arrMuta;
    
}

@end
